<?php

	class ModuleModel
	{
		public function __construct()
		{
			//parent::init(eb_get_module_file_vo("Core/ModuleFile"));
		}
		
		public function create()
		{
			return 1;
		}
		
		public function select()
		{
			$res = array();
		
			if ($handle = opendir(MODULES_PATH)) 
			{
				while (false !== ($file = readdir($handle))) 
				{
					if ($file == "." || $file == ".." || is_file($file)) continue;
					
					$item = eb_module_file_new_instance("Core/ModuleVO");
					
					$item->name = $file;
					
					$item->listModelFiles = $this->getFiles($file,MODULES_PATH.$file."/".FOLDER_MODEL,0);
					$item->listViewFiles = $this->getFiles($file,MODULES_PATH.$file."/".FOLDER_VIEW,1);
					$item->listControllerFiles = $this->getFiles($file,MODULES_PATH.$file."/".FOLDER_CONTROLLER,2);
					
					$res []= $item;
				}

				closedir($handle);
			}
			
			return $res;
		}
		
		public function insert($vo)
		{
			if (!is_dir(MODULES_PATH.$vo->name))
			{
				if (@mkdir(MODULES_PATH.$vo->name))
				{
					if (@mkdir(MODULES_PATH.$vo->name."/".FOLDER_MODEL) && 
						@mkdir(MODULES_PATH.$vo->name."/".FOLDER_VIEW) && 
						@mkdir(MODULES_PATH.$vo->name."/".FOLDER_CONTROLLER))
					{
						return true;
					}
				}
			}
			
			return false;
		}
		
		private function getFiles($module,$dir,$type)
		{
			$res = array();
		
			if ($handle = @opendir($dir)) 
			{
				while (false !== ($file = readdir($handle))) 
				{
					if ($file == "." || $file == ".." || is_dir($file)) continue;
					
					$item = eb_module_file_new_instance("Core/ModuleFileVO");
					
					$item->module = $module;
					$item->filename = $file;
					$item->type = $type;
									
					$res []= $item;
				}

				closedir($handle);
			}
			
			return $res;
		}
	}	